* plain & is invalid in XML
[lhc/web/wiklou.git] / includes / SpecialValidate.php
1 <?php
2 # Copyright (C) 2004 Magnus Manske <magnus.manske@web.de>
3 # http://www.mediawiki.org/
4 #
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License along
16 # with this program; if not, write to the Free Software Foundation, Inc.,
17 # 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 # http://www.gnu.org/copyleft/gpl.html
19
20 /**
21 *
22 * @package MediaWiki
23 * @subpackage SpecialPage
24 */
25
26 /**
27 *
28 * @package MediaWiki
29 * @subpackage SpecialPage
30 */
31 class Validation {
32 var $topicList;
33 var $voteCache;
34 var $rev2date;
35 var $date2ref;
36
37 # Reads all revision information of the specified article
38 function prepareRevisions( $id ) {
39 global $wgDBprefix;
40 $this->rev2date = array();
41 $this->date2rev = array();
42 $sql = "SELECT * FROM {$wgDBprefix}revision WHERE rev_page='{$id}'";
43 $res = wfQuery( $sql, DB_READ );
44 while( $x = wfFetchObject( $res ) ) {
45 $this->rev2date[$x->rev_id] = $x;
46 $this->date2rev[$x->rev_timestamp] = $x;
47 }
48 }
49
50 # Returns a HTML link to the specified article revision
51 function getVersionLink( &$article, $revision, $text = "" ) {
52 $t = $article->getTitle();
53 if( $text == "" ) $text = wfMsg("val_view_version");
54 $ret = "<a href=\"" . htmlspecialchars($t->getLocalURL( "oldid={$revision}" )) . "\">" . $text . "</a>";
55 return $ret;
56 }
57
58 # Returns an array containing all topics you can vote on
59 function getTopicList() {
60 global $wgDBprefix;
61 $ret = array();
62 $sql = "SELECT * FROM {$wgDBprefix}validate WHERE val_page=0";
63 $res = wfQuery( $sql, DB_READ );
64 while( $x = wfFetchObject( $res ) ) {
65 $ret[$x->val_type] = $x;
66 }
67 ksort( $ret );
68 return $ret;
69 }
70
71 # Merges one dataset into another
72 function mergeInto( &$source, &$dest ) {
73 $ret = false;
74 foreach( $source as $x => $y ) {
75 $doit = false;
76 if( !isset( $dest[$x] ) ) {
77 $doit = true;
78 } elseif( $dest[$x]->value == 0 ) {
79 $doit = true;
80 }
81 if( $doit ) {
82 $dest[$x] = $y;
83 $ret = true;
84 }
85 }
86 if( $ret ) {
87 ksort ( $dest );
88 }
89 return $ret;
90 }
91
92 # Merges all votes prior to the given revision into it
93 function mergeOldRevisions( &$article, $revision ) {
94 $tmp = $this->voteCache;
95 krsort( $tmp );
96 $update = false;
97 $ts = $this->getTimestamp( $revision );
98 $data = $this->voteCache[$ts];
99 foreach( $tmp as $x => $y ) {
100 if( $x < $ts ) {
101 if( $this->mergeInto( $y, $data ) ) {
102 $update = true;
103 }
104 }
105 }
106 if( $update ) {
107 $this->setRevision( $article, $revision, $data );
108 }
109 }
110
111 # Clears all votes prior to the given revision
112 function clearOldRevisions( &$article, $revision ) {
113 $tmp = $this->voteCache;
114 $ts = $this->getTimestamp( $revision );
115 foreach( $tmp as $x => $y ) {
116 if( $x < $ts ) {
117 $this->deleteRevision ( $article, $this->getRevisionNumber( $x ) );
118 }
119 }
120 }
121
122 # Updates the votes for the given revision from the FORM data
123 function updateRevision( &$article, $revision ) {
124 global $wgUser, $wgRequest;
125
126 if( isset( $this->voteCache[$this->getTimestamp( $revision )] ) ) {
127 $data = $this->voteCache[$this->getTimestamp( $revision )];
128 } else {
129 $data = array();
130 }
131 $nv = $wgRequest->getArray( "re_v_{$revision}", array() );
132 $nc = $wgRequest->getArray( "re_c_{$revision}", array() );
133
134 foreach( $nv as $x => $y ) {
135 $data[$x]->value = $y;
136 $data[$x]->comment = $nc[$x];
137 }
138 krsort( $data );
139
140 $this->setRevision( $article, $revision, $data );
141 }
142
143 # Sets a specific revision to both cache and database
144 function setRevision( &$article, $revision, &$data ) {
145 global $wgUser;
146 $this->deleteRevision( $article, $revision );
147 $this->voteCache[$this->getTimestamp( $revision )] = $data;
148 foreach( $data as $x => $y ) {
149 if( $y->value > 0 ) {
150 $ip = $wgUser->isAnon() ? $wgUser->getName() : '';
151 $dbw =& wfGetDB( DB_MASTER );
152 $dbw->insert( 'validate',
153 array(
154 'val_user' => $wgUser->getId(),
155 'val_page' => $article->getId(),
156 'val_revision' => $revision,
157 'val_type' => $x,
158 'val_value' => $y->value,
159 'val_comment' => $y->comment,
160 'val_ip' => $ip ),
161 'Validation::setRevision' );
162 }
163 }
164 }
165
166 # This function returns a MySQL statement to identify the current user
167 function identifyMe( $user = "" ) {
168 global $wgUser;
169 if( $user == "" ) $user = $wgUser->GetID();
170 if( User::isIP( $user ) ) {
171 return "(val_user='0' AND val_ip='{$user}')";
172 } else {
173 return "(val_user='{$user}')";
174 }
175 }
176
177 # Deletes a specific vote set in both cache and database
178 function deleteRevision( &$article, $revision ) {
179 global $wgUser, $wgDBprefix;
180 $ts = $this->getTimestamp( $revision );
181 if( !isset ( $this->voteCache[$ts] ) ) {
182 return; # Nothing to do
183 }
184 $sql = "DELETE FROM {$wgDBprefix}validate WHERE" . $this->identifyMe() . " AND ";
185 $sql .= " val_page='" . $article->getID() . "' AND val_revision='{$revision}'";
186 $res = wfQuery( $sql, DB_WRITE );
187 unset( $this->voteCache[$ts] );
188 }
189
190 # Reads the entire vote list for this user for the given article
191 function getVoteList( $id, $user = "" ) {
192 global $wgUser, $wgDBprefix;
193 if( $user == "" ) {
194 $user = $wgUser->GetID();
195 }
196 $r = array() ; # Revisions
197 $sql = "SELECT * FROM {$wgDBprefix}validate WHERE val_page=" . $id . " AND " . $this->identifyMe( $user );
198 $res = wfQuery( $sql, DB_READ );
199 while( $x = wfFetchObject( $res ) ) {
200 #$y = $x->val_revision;
201 $y = $this->rev2date[$x->val_revision];
202 $y = $y->rev_timestamp;
203 if( !isset( $r[$y] ) ) {
204 $r[$y] = array();
205 }
206 $r[$y][$x->val_type]->value = $x->val_value;
207 $r[$y][$x->val_type]->comment = $x->val_comment;
208 }
209 return $r;
210 }
211
212 # Reads the entire vote list for this user for all articles
213 function getAllVoteLists( $user ) {
214 global $wgDBprefix;
215 $r = array() ; # Revisions
216 $sql = "SELECT * FROM {$wgDBprefix}validate WHERE " . $this->identifyMe( $user );
217 $res = wfQuery( $sql, DB_READ );
218 while( $x = wfFetchObject( $res ) ) {
219 $a = $x->val_page;
220 $y = $x->val_revision;
221 if( !isset( $r[$a] ) ) {
222 $r[$a] = array();
223 }
224 if( !isset( $r[$a][$y] ) ) {
225 $r[$a][$y] = array();
226 }
227 $r[$a][$y][$x->val_type] = $x;
228 }
229 return $r;
230 }
231
232 # This functions adds a topic to the database
233 function addTopic( $topic, $limit ) {
234 global $wgDBprefix;
235 $a = 1;
236 while( isset( $this->topicList[$a] ) ) {
237 $a++;
238 }
239 $sql = "INSERT INTO {$wgDBprefix}validate (val_user,val_page,val_revision,val_type,val_value,val_comment,val_ip) VALUES (";
240 $sql .= "'0','0','0','{$a}','{$limit}','";
241 $sql .= Database::strencode( $topic ) . "','')";
242 $res = wfQuery( $sql, DB_WRITE );
243 $x->val_user = $x->val_page = $x->val_revision = 0;
244 $x->val_type = $a;
245 $x->val_value = $limit;
246 $x->val_comment = $topic;
247 $x->val_ip = "";
248 $this->topicList[$a] = $x;
249 ksort( $this->topicList );
250 }
251
252 # This functions adds a topic to the database
253 function deleteTopic( $id ) {
254 global $wgDBprefix;
255 $dbw =& wfGetDB( DB_MASTER );
256 $dbw->delete( 'validate',
257 array( 'val_type' => $id ),
258 'Validation::deleteTopic' );
259 unset( $this->topicList[$id] );
260 }
261
262 # This function returns a link text to the page validation statistics
263 function link2statistics( &$article ) {
264 $nt = $article->getTitle();
265 $url = $nt->escapeLocalURL( "action=validate&mode=list" );
266 return wfMsg( 'val_rev_stats_link', $nt->getPrefixedText(), $url );
267 }
268
269 # This function returns a link text to the page validation statistics of a single revision
270 function link2revisionstatistics( &$article, $revision ) {
271 $nt = $article->getTitle();
272 $url = $nt->escapeLocalURL( "action=validate&mode=details&revision={$revision}" );
273 return wfMsg( 'val_revision_stats_link', $url );
274 }
275
276 # This function returns a link text to the user rating statistics page
277 function link2userratings( $user, $text ) {
278 global $wgUser;
279 if( $user == 0 ) {
280 $user = $wgUser->GetName();
281 }
282 $nt = Title::newFromText( "Special:Validate" );
283 $url = htmlspecialchars( $nt->getLocalURL( "mode=userstats&user={$user}" ) );
284 return "<a href=\"{$url}\">{$text}</a>";
285 }
286
287 # Returns the timestamp of a revision based on the revision number
288 function getTimestamp( $revision ) {
289 $ts = $this->rev2date[$revision];
290 $ts = $ts->rev_timestamp;
291 return $ts;
292 }
293
294 # Returns the revision number of a revision based on the timestamp
295 function getRevisionNumber( $ts ) {
296 $revision = $this->date2rev[$ts];
297 $revision = $revision->rev_id;
298 return $revision;
299 }
300
301
302 # HTML generation functions from this point on
303
304 # Returns the metadata string for a revision
305 function getMetadata( $idx ) {
306 $metadata = "";
307 $x = $this->rev2date[$idx];
308 $metadata .= wfTimestamp( TS_DB, $x->rev_timestamp );
309 $metadata .= " by ";
310 if( $x->rev_user == 0 ) {
311 $metadata .= $x->rev_user_text;
312 } else {
313 $u = new User;
314 $u->setId( $x->rev_user );
315 $u->setName( $x->rev_user_text );
316 $nt = $u->getUserPage();
317 $url = "<a href='" . $nt->getLocalUrl() . "'>" . $nt->getText() . "</a>";
318 $metadata .= $url;
319 }
320 $metadata .= " : <small>\"" . htmlspecialchars( $x->rev_comment ) . "\"</small>";
321 return $metadata;
322 }
323
324 # Generates a link to the topic description
325 function linkTopic ( $s ) {
326 $t = Title::newFromText ( wfMsg ( 'val_topic_desc_page' ) ) ;
327 $r = "<a href=\"" ;
328 $r .= $t->getLocalURL () ;
329 $r .= "#" . urlencode ( $s ) ;
330 $r .= "\">{$s}</a>" ;
331 return $r ;
332 }
333
334 # Generates a form for a single revision
335 function getRevisionForm( &$article, $idx, &$data, $focus = false ) {
336 # Fill data with blank values
337 $ts = $idx;
338 $revision = $this->getRevisionNumber( $ts );
339 foreach( $this->topicList as $x => $y ) {
340 if( !isset( $data[$x] ) ) {
341 $data[$x]->value = 0;
342 $data[$x]->comment = "";
343 }
344 }
345 ksort( $data ) ;
346
347 # Generate form
348 $ret = "<form method='post'>";
349 $ret .= "<table border='1' cellspacing='0' cellpadding='2'";
350 if( $focus ) {
351 $ret .= " style='background-color:#00BBFF'";
352 }
353 $ret .= ">\n";
354 $head = "Revision #" . $revision;
355 $link = " " . $this->getVersionLink( $article, $revision );
356 $metadata = $this->getMetadata( $revision );
357 $ret .= "<tr><th align='left' colspan='3'>" . $head . " ({$link}) {$metadata}</th></tr>\n";
358 $line = 0;
359 foreach( $data as $x => $y ) {
360 $line = 1 - $line;
361 $col = $line == 1 ? "#DDDDDD" : "#EEEEEE";
362 $idx = "_{$revision}[{$x}]";
363 $ret .= "<tr bgcolor='{$col}'>\n";
364 $ret .= "<th nowrap>";
365 $ret .= $this->linkTopic ( $this->topicList[$x]->val_comment ) ;
366 $ret .= "</th>\n";
367
368 $tlx = $this->topicList[$x];
369 $vote = "";
370 $max = $tlx->val_value;
371 for( $a = 0 ; $a <= $max ; $a++ ) {
372 if( $a == 0 ) {
373 $vote .= wfMsg ( "val_noop" );
374 }
375 $vote .= "<input type='radio' name='re_v{$idx}' value='{$a}'";
376 if( $a == $y->value ) {
377 $vote .= " checked";
378 }
379 $vote .= "/>";
380 if( $max == 2 && $a == 1 ) {
381 $vote .= wfMsg( "val_no" ) . " ";
382 } elseif( $max == 2 && $a == 2 ) {
383 $vote .= wfMsg( "val_yes" );
384 } elseif( $a != 0 ) {
385 $vote .= $a . " ";
386 }
387 if ( $a == 0 ) {
388 $vote .= " &nbsp; ";
389 }
390 }
391 $ret .= "<td nowrap valign='center'>{$vote}</td>\n";
392
393 $ret .= "<td width='100%' align='center'><input size='50' style='width:98%' maxlength='250' type='text' name='re_c{$idx}' value='{$y->comment}'/>";
394 $ret .= "</td></tr>\n";
395 }
396 $checked = $focus ? " checked='checked'" : "";
397 $ret .= "<tr><td colspan='3' valign='center'>\n";
398 $ret .= "<input type='checkbox' name='re_merge_{$revision}' value='1'{$checked}/>" . htmlspecialchars( wfMsg( 'val_merge_old' ) ) . " \n";
399 $ret .= "<input type='checkbox' name='re_clear_{$revision}' value='1'{$checked}/>" . htmlspecialchars( wfMsg( 'val_clear_old' ) ) . " \n";
400 $ret .= "<input type='submit' name='re_submit[{$revision}]' value='" . htmlspecialchars( wfMsg("ok") ) . "'/>\n";
401 if( $focus ) {
402 $ret .= "<br/>\n<small>" . htmlspecialchars( wfMsg( "val_form_note" ) ) . "</small>";
403 }
404 $ret .= "</td></tr>\n";
405 $ret .= "</table>\n</form>\n\n";
406 return $ret;
407 }
408
409
410 # Generates the page from the validation tab
411 function validatePageForm( &$article, $revision ) {
412 global $wgOut, $wgRequest, $wgUser;
413
414 $ret = "";
415 $this->prepareRevisions( $article->getID() );
416 $this->topicList = $this->getTopicList();
417 $this->voteCache = $this->getVoteList( $article->getID() );
418
419 # Check for POST data
420 $re = $wgRequest->getArray( 're_submit' );
421 if ( isset( $re ) ) {
422 $id = array_keys( $re );
423 $id = $id[0] ; # $id is now the revision number the user clicked "OK" for
424 $clearOldRev = $wgRequest->getVal( "re_clear_{$id}", 0 );
425 $mergeOldRev = $wgRequest->getVal( "re_merge_{$id}", 0 );
426 $this->updateRevision( $article, $id );
427 if( $mergeOldRev ) {
428 $this->mergeOldRevisions( $article, $id );
429 }
430 if( $clearOldRev ) {
431 $this->clearOldRevisions( $article, $id );
432 }
433 $ret .= "<p><font color='red'>" . htmlspecialchars( wfMsg( 'val_revision_changes_ok' ) ) . "</font></p>";
434 }
435 else $ret .= wfMsg ( 'val_votepage_intro' ) ;
436
437 # Make sure the requested revision exists
438 $ts = $this->rev2date[$revision]->rev_timestamp;
439 if( !isset( $this->voteCache[$ts] ) ) {
440 $this->voteCache[$ts] = array();
441 }
442
443 # Sort revisions list, newest first
444 krsort( $this->voteCache );
445
446 # Output
447 $title = $article->getTitle();
448 $title = $title->getPrefixedText();
449 $wgOut->setPageTitle( wfMsg( 'val_rev_for' ) . $title );
450 foreach( $this->voteCache as $x => $y ) {
451 $ret .= $this->getRevisionForm( $article, $x, $y, $x == $ts );
452 $ret .= "<br/>\n";
453 }
454 $ret .= $this->link2statistics( $article );
455 $ret .= "<p>" . $this->link2userratings( $wgUser->getID(), wfMsg( 'val_show_my_ratings' ) ) . "</p>";
456 return $ret ;
457 }
458
459 # This function performs the "management" mode on Special:Validate
460 function manageTopics() {
461 global $wgRequest;
462 $this->topicList = $this->getTopicList();
463
464 $iamsure = $wgRequest->getVal( "iamsure", "0" ) == 1;
465
466 if( $iamsure && $wgRequest->getVal( "m_add", "--" ) != "--" ) {
467 $new_topic = $wgRequest->getVal( "m_topic" );
468 $new_limit = $wgRequest->getVal( "m_limit" );
469 if( $new_topic != "" && $new_limit > 1 ) {
470 $this->addTopic( $new_topic, $new_limit );
471 }
472 }
473
474 $da = $wgRequest->getArray( "m_del" );
475 if( $iamsure && isset( $da ) && count( $da ) > 0 ) {
476 $id = array_keys( $da );
477 $id = array_shift( $id );
478 $this->deleteTopic( $id );
479 }
480
481 # FIXME: Wikitext this
482 $r = "<p>" . htmlspecialchars( wfMsg( 'val_warning' ) ) . "</p>\n";
483
484 $r .= "<form method='post'>\n";
485 $r .= "<table border='1' cellspacing='0' cellpadding='2'>\n";
486 $r .= "<tr>" . wfMsg( 'val_list_header' ) . "</tr>\n";
487 foreach( $this->topicList as $x => $y ) {
488 $r .= "<tr>\n";
489 $r .= "<th>" . htmlspecialchars( $y->val_type ) . "</th>\n";
490 $r .= "<td>" . $this->linkTopic ( $y->val_comment ) . "</td>\n";
491 $r .= "<td>1 .. <b>" . intval( $y->val_value ) . "</b></td>\n";
492 $r .= "<td><input type='submit' name='m_del[" . intval( $x ) . "]' value='" . htmlspecialchars( wfMsg( 'val_del' ) ) . "'/></td>\n";
493 $r .= "</tr>\n";
494 }
495 $r .= "<tr>\n";
496 $r .= "<td/>\n";
497 $r .= "<td><input type='text' name='m_topic' value=''/></td>\n";
498 $r .= "<td><input type='text' name='m_limit' value=''/></td>\n";
499 $r .= "<td><input type='submit' name='m_add' value='" . htmlspecialchars( wfMsg( 'val_add' ) ) . "'/></td>\n";
500 $r .= "</tr>\n";
501 $r .= "</table>\n";
502 $r .= "<input type='checkbox' name='iamsure' value='1'/>" . htmlspecialchars( wfMsg( 'val_iamsure' ) ) . "\n";
503 $r .= "</form>\n";
504 return $r;
505 }
506
507 # Generates a user ID for both logged-in users and anons; $x is an object from an SQL query
508 function make_user_id( &$x ) {
509 if( $x->val_user == 0 ) {
510 return $x->val_ip;
511 } else {
512 return $x->val_user;
513 }
514 }
515
516 function showDetails( &$article, $revision ) {
517 global $wgDBprefix, $wgOut, $wgUser;
518 $this->prepareRevisions( $article->getID() );
519 $this->topicList = $this->getTopicList();
520
521 $title = $article->getTitle();
522 $wgOut->setPageTitle( str_replace( '$1', $title->getPrefixedText(), wfMsg( 'val_validation_of' ) ) );
523
524 # Collecting statistic data
525 $id = $article->getID();
526 $sql = "SELECT * FROM {$wgDBprefix}validate WHERE val_page='{$id}' AND val_revision='{$revision}'";
527 $res = wfQuery( $sql, DB_READ );
528 $data = array();
529 $users = array();
530 $topics = array();
531 while( $x = wfFetchObject( $res ) ) {
532 $data[$this->make_user_id($x)][$x->val_type] = $x;
533 $users[$this->make_user_id($x)] = true;
534 $topics[$x->val_type] = true;
535 }
536
537 # Sorting lists of topics and users
538 ksort( $users );
539 ksort( $topics );
540
541 $ts = $this->getTimestamp( $revision );
542 $url = $this->getVersionLink( $article, $revision, wfTimestamp( TS_DB, $ts ) );
543
544 # Table headers
545 $ret = "" ;
546 $ret .= "<p><b>" . str_replace( '$1', $url, wfMsg( 'val_revision_of' ) ) . "</b></p>\n";
547 $ret .= "<table border='1' cellspacing='0' cellpadding='2'>\n";
548 $ret .= "<tr><th/>";
549
550 foreach( $topics as $t => $dummy ) {
551 $ret .= "<th>" . $this->linkTopic ( $this->topicList[$t]->val_comment ) . "</th>";
552 }
553 $ret .= "</tr>\n";
554
555 # Table data
556 foreach( $users as $u => $dummy ) { # Every row a user
557 $ret .= "<tr>";
558 $ret .= "<th align='left'>";
559 if( !User::IsIP( $u ) ) { # Logged-in user rating
560 $ret .= $this->link2userratings( $u, User::whoIs( $u ) );
561 } else { # Anon rating
562 $ret .= $this->link2userratings( $u, $u );
563 }
564 $ret .= "</th>";
565 foreach( $topics as $t => $dummy ) { # Every column a topic
566 if( !isset( $data[$u][$t] ) ) {
567 $ret .= "<td/>";
568 } else {
569 $ret .= "<td valign='center'>";
570 $ret .= $data[$u][$t]->val_value;
571 if( $data[$u][$t]->val_comment != "" ) {
572 $ret .= " <small>(" . htmlspecialchars( $data[$u][$t]->val_comment ) . ")</small>";
573 }
574 $ret .= "</td>";
575 }
576 }
577 $ret .= "</tr>";
578 }
579 $ret .= "</table>";
580 $ret .= "<p>" . $this->link2statistics( $article ) . "</p>";
581 $ret .= "<p>" . $this->link2userratings( $wgUser->GetID(), wfMsg( 'val_show_my_ratings' ) ) . "</p>";
582
583 return $ret;
584 }
585
586 function showList( &$article ) {
587 global $wgDBprefix, $wgOut, $wgUser;
588 $this->prepareRevisions( $article->getID() );
589 $this->topicList = $this->getTopicList();
590
591 $title = $article->getTitle();
592 $wgOut->setPageTitle( str_replace( '$1', $title->getPrefixedText(), wfMsg( 'val_validation_of' ) ) );
593
594 # Collecting statistic data
595 $id = $article->getID();
596 $sql = "SELECT * FROM {$wgDBprefix}validate WHERE val_page='{$id}'";
597 $res = wfQuery( $sql, DB_READ );
598 $data = array();
599 while( $x = wfFetchObject( $res ) ) {
600 $idx = $this->getTimestamp( $x->val_revision );
601 if( !isset( $data[$idx] ) ) {
602 $data[$idx] = array();
603 }
604 if( !isset( $data[$idx][$x->val_type] ) ) {
605 $data[$idx][$x->val_type]->count = 0;
606 $data[$idx][$x->val_type]->sum = 0;
607 }
608 $data[$idx][$x->val_type]->count++;
609 $data[$idx][$x->val_type]->sum += $x->val_value;
610 }
611
612 krsort( $data );
613
614 $ret = "";
615 $ret .= "<table border='1' cellspacing='0' cellpadding='2'>\n";
616 $ret .= "<tr><th>" . htmlspecialchars( wfMsg( "val_revision" ) ) . "</th>";
617 foreach( $this->topicList as $x => $y ) {
618 $ret .= "<th>" . $this->linkTopic ( $y->val_comment ) . "</th>";
619 }
620 $ret .= "</tr>\n";
621 foreach( $data as $ts => $y ) {
622 $revision = $this->getRevisionNumber( $ts );
623 $url = $this->getVersionLink( $article, $revision, wfTimestamp( TS_DB, $ts ) );
624 $detailsurl = $this->link2revisionstatistics( $article, $revision );
625 $ret .= "<tr><td>{$url} {$detailsurl}</td>";
626 foreach( $this->topicList as $topicID => $dummy ) {
627 if( isset( $y[$topicID] ) ) {
628 $z = $y[$topicID];
629 if( $z->count == 0 ) {
630 $a = 0;
631 } else {
632 $a = $z->sum / $z->count;
633 }
634 $ret .= sprintf( "<td><b>%1.1f</b> (%d)</td>", $a, $z->count );
635 } else {
636 $ret .= "<td></td>";
637 }
638 }
639 $ret .= "</tr>\n";
640 }
641 $ret .= "</table>\n";
642 $ret .= "<p>" . $this->link2userratings( $wgUser->getID(), wfMsg( 'val_show_my_ratings' ) ) . "</p>";
643 return $ret;
644 }
645
646 function getRatingText( $value, $max ) {
647 if( $max == 2 && $value == 1 ) {
648 $ret = wfMsg ( "val_no" ) . " ";
649 } elseif( $max == 2 && $value == 2 ) {
650 $ret = wfMsg( "val_yes" );
651 } elseif( $value != 0 ) {
652 $ret = wfMsg( "val_of", $value, $max ) . " ";
653 } else {
654 $ret = "";
655 }
656 return $ret;
657 }
658
659 function showUserStats( $user ) {
660 global $wgDBprefix, $wgOut, $wgUser;
661 $this->topicList = $this->getTopicList();
662 $data = $this->getAllVoteLists( $user );
663
664 if( $user == $wgUser->getID() ) {
665 $wgOut->setPageTitle ( wfMsg ( 'val_my_stats_title' ) );
666 } elseif( !User::IsIP( $user ) ) {
667 $wgOut->setPageTitle( wfMsg( 'val_user_stats_title', User::whoIs( $user ) ) );
668 } else {
669 $wgOut->setPageTitle( wfMsg( 'val_user_stats_title', $user ) );
670 }
671
672 $ret = "<table border='1' cellspacing='0' cellpadding='2'>\n";
673
674 foreach( $data as $articleid => $revisions ) {
675 $title = Title::newFromID( $articleid );
676 $ret .= "<tr><th align='left' colspan='4'><a href=\"" . $title->escapeLocalURL() . "\">" . $title->getEscapedText() . "</a></th></tr>";
677 krsort( $revisions );
678 foreach( $revisions as $revid => $revision ) {
679 $url = $title->getLocalURL( "oldid={$revid}" );
680 $ret .= "<tr><th align='left'><a href=\"{$url}\">" . wfMsg( 'val_revision_number', $revid ) . "</a></th>";
681 ksort( $revision );
682 $initial = true;
683 foreach( $revision as $topic => $rating ) {
684 if( !$initial ) {
685 $ret .= "<tr><td/>";
686 }
687 $initial = false;
688 $ret .= "<td>" . $this->linkTopic ( $this->topicList[$topic]->val_comment ) . "</td>";
689 $ret .= "<td>" . $this->getRatingText( $rating->val_value, $this->topicList[$topic]->val_value ) . "</td>";
690 $ret .= "<td>" . htmlspecialchars( $rating->val_comment ) . "</td>";
691 $ret .= "</tr>";
692 }
693 }
694 $ret .= "</tr>";
695 }
696 $ret .= "</table>";
697
698 return $ret;
699 }
700
701 }
702
703 /**
704 * constructor
705 */
706 function wfSpecialValidate( $page = '' ) {
707 global $wgOut, $wgRequest, $wgUseValidation, $wgUser, $wgContLang;
708
709 if( !$wgUseValidation ) {
710 $wgOut->errorpage( "nosuchspecialpage", "nospecialpagetext" );
711 return;
712 }
713
714 /*
715 # Can do?
716 if( ! $wgUser->isAllowed('change_validation') ) {
717 $wgOut->sysopRequired();
718 return;
719 }
720 */
721
722 $mode = $wgRequest->getVal( "mode" );
723 $skin = $wgUser->getSkin();
724
725
726 if( $mode == "manage" ) {
727 $v = new Validation();
728 $html = $v->manageTopics();
729 } elseif( $mode == "userstats" ) {
730 $v = new Validation();
731 $user = $wgRequest->getVal( "user" );
732 $html = $v->showUserStats( $user );
733 } else {
734 $html = "$mode";
735 $html .= "<ul>\n";
736
737 $t = Title::newFromText( "Special:Validate" );
738 $url = $t->escapeLocalURL( "mode=manage" );
739 $html .= "<li><a href=\"" . $url . "\">Manage</a></li>\n";
740
741 $html .= "</ul>\n";
742 }
743
744 $wgOut->addHTML( $html );
745 }
746
747 ?>